﻿using KINLO.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.SqlClient;
using KINLO.Model.Dto;

namespace KINLO.Data.Reporitory
{
    public class MachineReporitory
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WeightReporitory));
        Database DB;
        public MachineReporitory()
        {
            DB = (new DbContext("name=WeightDB")).Database;
        }

        /// <summary>
        ///  根据ID查机器
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Machine GetMachineByID(Guid guid)
        {
            try
            {
                var cmd = @"
                    SELECT ID ,Name , MachineType,IP ,Port ,Remark 
                    FROM dbo.Machines WHERE ID='@ID'";
                return DB.SqlQuery<Machine>(cmd,
                    new SqlParameter("@ID", guid)).ToList().FirstOrDefault();

            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "GetMachineByID", ex));
                return null;
            }
        }

        /// <summary>
        ///  所有设备
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<Machine> GetAllMachines()
        {
            try
            {
                var cmd = @"SELECT ID ,Name ,MachineType ,IP ,Port ,Remark FROM dbo.Machines 
                            ORDER BY MachineType,Port,Name";
                return DB.SqlQuery<Machine>(cmd).ToList();

            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "GetMachineByID", ex));
                return null;
            }
        }
        /// <summary>
        ///  所有设备   测试用
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<MachineDto> GetAllMachines2()
        {
            try
            {
                var cmd = @"SELECT ID ,Name ,MachineType ,IP ,Port ,Remark FROM dbo.Machines 
                            ORDER BY MachineType,Port,Name";
                return DB.SqlQuery<MachineDto>(cmd).ToList();

            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "GetMachineByID", ex));
                return null;
            }
        }

        /// <summary>
        ///  根据类型查询
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<Machine> GetMachineByMachineType(int type)
        {
            try
            {
                var cmd = @"SELECT * FROM dbo.Machines WHERE MachineType=@Type or 0=@Type";
                var A = DB.SqlQuery<Machine>(cmd,
                    new SqlParameter("@Type", type.ToString())).ToList();
                return A;

            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "GetMachineByMachineType", ex));
                log.Error(666666666666666666+"类型"+type);
                return null;
            }
        }
        /// <summary>
        /// 新增机器
        /// </summary>
        /// <param name="weight"></param>
        /// <returns></returns>
        public int AddMachine(Machine machine)
        {
            try
            {
                var cmd = @" INSERT dbo.Machines
                        ( ID, Name, MachineType, IP, Port, Remark)
                        VALUES( @ID,  @Name,  @MachineType,  @IP,   @Port,   @Remark   )";

                return DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@ID", machine.ID),
                    new SqlParameter("@Name", machine.Name), 
                    new SqlParameter("@MachineType", machine.MachineType),
                    new SqlParameter("@IP", machine.IP??""),
                    new SqlParameter("@Port", machine.Port),
                    new SqlParameter("@Remark", machine.Remark??"")); 
            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "AddMachine", ex));
                return 0;

            }

        }
        /// <summary>
        /// 修改机器
        /// </summary>
        /// <param name="weight"></param>
        /// <returns></returns>
        public int UpdateMachine(Machine machine)
        {
            try
            {
                var cmd = @"
                    UPDATE Machines
                    SET Name = @Name,  MachineType = @MachineType, IP = @IP, Remark = @Remark,Port=@Port
                    WHERE ID = @ID";
               return DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@ID", machine.ID),
                    new SqlParameter("@Name", machine.Name), 
                    new SqlParameter("@MachineType", machine.MachineType),
                    new SqlParameter("@IP", machine.IP ?? ""),
                    new SqlParameter("@Port", machine.Port ),
                    new SqlParameter("@Remark", machine.Remark??"")
                    ); 
            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "UpdateMachine", ex));
                return 0;
            }

        }

        /// <summary>
        /// 删除机器
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public int DeleteMachine(Guid guid)
        {
            try
            {
                var cmd = string.Format(" DELETE dbo.Machines WHERE ID = @ID");
                return DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@ID", guid)); 
            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "DeleteMachine", ex));
                return 0;
            }
        }

        /// <summary>
        /// 删除机器
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public int DeleteMachineByMachineType(int machType)
        {
            try
            {
                var cmd = string.Format(" DELETE dbo.Machines WHERE MachineType=@MachineType");
                return DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@MachineType", machType));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("class:{0},method:{1},发生了错误:{2}", "MachineReporitory", "DeleteMachineByMachineType", ex));
                return 0;
            }
        }
    }
}
